package com.singing.manager;

import com.singing.dao.SingScoreDao;
import com.singing.dao.SingerDao;
import com.singing.model.Command;
import com.singing.model.SingScore;
import com.singing.model.Singer;

import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class SingleSingerScoreManager extends BaseManager {


    private static final int INPUT_SIGNER_ID = 1;
    SingScoreDao singScoreDao = new SingScoreDao();
    SingerDao singerDao = new SingerDao();

    public SingleSingerScoreManager(String title, List<Command> commandList) {
        super(title, commandList);
    }


    @Override
    protected int processCommand(int command) {
        switch (command) {
            case INPUT_SIGNER_ID:
                showInputSingerId();
                break;
        }
        return 0;
    }

    @Override
    protected void printTitleExtra() {
        System.out.println("歌手id\t\t歌手名称\t\t歌手性别");
        showSingerList();
    }


    private void showInputSingerId() {
        while (true) {
            System.out.println("请输入歌手id：");
            Scanner scanner = new Scanner(System.in);
            Integer singerId = null;
            try {
                singerId = scanner.nextInt();
                showSingerScore(singerId);
                break;
            } catch (Exception e) {
                System.out.println("输入错误，请重新输入");
            }
        }
    }

    private void showSingerScore(Integer singerId) {

        List<SingScore> singScores = singScoreDao.selectListBySingerId(singerId);
        Singer singer = singerDao.selectById(singerId);

        if (singer == null) {
            System.err.println("没有该歌手");
            return;
        }

        // 判断是否为空
        if (singScores == null || singScores.size() <= 0) {
            System.err.println("该歌手还没有被评委评分");
            return;
        }
        //展示歌手评分
        System.out.println("歌手id\t\t歌手名称\t\t平均分（去除最高分和最低分）\t\t总分（去除最高分和最低分）");
        System.out.println();

        // 计算平均分，使用stream

        List<SingScore> sorted = singScores.stream().sorted(Comparator.comparingInt(SingScore::getScore)).collect(Collectors.toList());
        // 去掉最高分和最低分
        sorted.remove(0);

        sorted.remove(sorted.size() - 1);

        // 计算平均分,使用stream
        List<Integer> scores = sorted.stream().map(SingScore::getScore).collect(Collectors.toList());
        // 求和
        int sum = scores.stream().reduce(0, Integer::sum);

        double avgScore = (double) sum / scores.size();

        System.out.println(singer.getId() + "\t\t" + singer.getName() + "\t\t" + avgScore + "\t\t" + sum);
        System.out.println();

        System.out.println("按回车退出");
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();
    }

    private void showSingerList() {
        List<Integer> ids = singScoreDao.selectAllSingerId();
        List<Singer> singers = null;
        if (ids == null || ids.size() <= 0) {
            System.err.println("还没有歌手被评委评分");
            return;
        }

        singers = singerDao.selectByIds(ids);

        Scanner scanner = new Scanner(System.in);
        for (Singer singer : singers) {
            System.out.println(singer.getId() + "\t\t" + singer.getName() + "\t\t" + (singer.getGender() == 0 ? "男" : "女"));
        }
    }

}
